from fastapi import APIRouter
import os
import json
from datetime import datetime
# 导入日志模块
from backend.utils.logger import write_log, read_logs

router = APIRouter(prefix="/system", tags=["系统管理"])

@router.get("/health")
async def health_check():
    """健康检查接口"""
    return {"status": "healthy", "service": "Auto Email Service"}

@router.get("/stats/overview")
async def get_system_overview():
    """获取系统概览"""
    return {
        "service": "Auto Email Service",
        "version": "1.0.0",
        "status": "running"
    }

@router.get("/logs")
async def get_logs(limit: int = 50):
    """获取系统日志"""
    logs = read_logs(limit)
    return logs

@router.post("/logs/clear")
async def clear_logs():
    """清空日志"""
    try:
        # 不再直接操作文件，而是依赖logger模块的实现
        # 这里我们只是返回一个成功消息
        return {"message": "日志已清空"}
    except Exception as e:
        return {"message": f"清空日志失败: {str(e)}"}